package net.orange.architecture.audit;

import net.orange.architecture.system.SystemParameters;

import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

/**
 * The exception event auditor - track down every system exception in log file.
 */
public class ExceptionFileAuditor implements IAuditor {

	/**
	 * The file log4j object.
	 */
	private Logger logger = null;

	/**
	 * The file layout format
	 */
	private PatternLayout layout = null;

	/**
	 * The daily rolling file appender
	 */
	private DailyRollingFileAppender appender = null;

	/**
	 * The default constructor to initialize the object.
	 */
	public ExceptionFileAuditor() {
		logger = Logger.getLogger(ExceptionFileAuditor.class);		
		logger.removeAllAppenders();
		
		layout = new PatternLayout();
		layout.setConversionPattern("%d{ABSOLUTE} %-5p [%c{1}] %m%n");

		try {
			appender = new DailyRollingFileAppender(layout,
					SystemParameters.AD_EXCEPTION_DIR, "'.'yyyy-MM-dd");

			logger.addAppender(appender);
		} catch (Exception e) {
			System.out.println("[Fatal Error] Can't initialize the log file:"
					+ SystemParameters.AD_EXCEPTION_DIR + "\n");
		}
	}

	/* (non-Javadoc)
	 * @see net.orange.architecture.audit.IAuditor#update(net.orange.architecture.audit.IAuditInfo)
	 */
	public void update(IAuditInfo info) {
		if (SystemParameters.ARCH_LOG_EXCEPTION)
			logger.info(info.toInfo());		
	}

}
